﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Reflection;
using System.IO;

namespace Security.Encryption
{
    public class CertificateHelper
    {
        public static X509Certificate2 GetCertificate(Stream strmCertificate, string password)
        {
            if (strmCertificate != null)
                return new X509Certificate2(ReadFully(strmCertificate), password);
            return null;
        }

        private static byte[] ReadFully(Stream stream)
        {
            byte[] buffer = new byte[32768];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }
    }
}
